import pandas as pd
import statsmodels.api as sm
import numpy as np

df = pd.read_csv('合并RID.csv', encoding='gbk')

def modifyCol(colName, val):
    for index, row in df.iterrows():
        if row[colName] in val:
            df.loc[index, colName] = 1
        else:
            df.loc[index, colName] = 0
modifyCol('TYPE', ['SMC', 'EMCI', 'LMCI'])

# 对所有数值列逻辑回归
allCol = df.columns.tolist()[10:]

def logit(XColName, YColName):
    print('\nX:', XColName)
    Y = np.asarray(df[YColName]).astype(np.float64)
    X = np.asarray(df[XColName])
    logit = sm.Logit(Y, X)
    result = logit.fit()
    print(result.summary())

for XColName in allCol:
    if np.issubdtype(df[XColName].dtype, np.number):
        if df[XColName].isnull().any():
            print(XColName, 'has NaN\n')
        else:
            logit(XColName, 'TYPE')